﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Common.ControlDevice;

namespace Common.Tool
{
    public class VersionHandle
    {
        // 对应版本信息
        public string Version { get; set; }
        // 对应内部版本信息
        public string InternalVersion { get; set; }
        // 对应版本用途类型
        public string VersionType { get; set; }
        // 对应版本编译时间
        public string BuildTime { get; set; }
        // 权利信息
        public string CopyRightMessage { get; set; }
        // 对应版本信息列表
        public List<string> SubSystemVersionList { get; set; }

        /**
         * \brief 获取更新数据
         */
        public void GetVersionData(Assembly assembly, List<ErrorInfo> errorList)
        {
            try
            {
                AssemblyFileVersionAttribute fileVersion = (AssemblyFileVersionAttribute)
                    AssemblyFileVersionAttribute.GetCustomAttribute(assembly, typeof(AssemblyFileVersionAttribute));
                Version = fileVersion.Version.ToString();

                AssemblyInformationalVersionAttribute internalVersion = (AssemblyInformationalVersionAttribute)
                    AssemblyInformationalVersionAttribute.GetCustomAttribute(assembly, typeof(AssemblyInformationalVersionAttribute));
                InternalVersion = internalVersion.InformationalVersion.ToString();

                Version timeVersion = assembly.GetName().Version;
                DateTime dateTime = new DateTime(2000, 1, 1);
                BuildTime = dateTime.AddDays(timeVersion.Build).AddSeconds(timeVersion.Revision * 2).ToString("yyyy/MM/dd HH:mm:ss");

                // 软件权利信息 2021.03.03 wj 增
                AssemblyCopyrightAttribute asmcpr = (AssemblyCopyrightAttribute)Attribute.GetCustomAttribute(assembly, typeof(AssemblyCopyrightAttribute));
                CopyRightMessage = asmcpr.Copyright.ToString();

                // 对应版本类型  2020.12.02 wj 增
                object[] tradeMarkAttributes = assembly.GetCustomAttributes(typeof(AssemblyTrademarkAttribute), false);
                if (0 != tradeMarkAttributes.Length)
                {
                    VersionType = ((AssemblyTrademarkAttribute)tradeMarkAttributes[0]).Trademark;
                }
                else
                {
                    errorList.Add(new ErrorInfo(ErrorLevelCategory.Error, "版本类别解析错误"));
                }

                // 对应发布文档版本号  2020.12.02 wj 增
                object[] configurationAttributes = assembly.GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false);
                if (0 != configurationAttributes.Length)
                {
                    SubSystemVersionList = ((AssemblyConfigurationAttribute)configurationAttributes[0]).Configuration.Trim().Split(',').ToList();
                }
                else
                {
                    errorList.Add(new ErrorInfo(ErrorLevelCategory.Error, "发布文档版本号解析错误"));
                }
            }
            catch (Exception ex)
            {
                errorList.Add(new ErrorInfo(ErrorLevelCategory.Error, "发布版本信息解析错误" + ex.ToString()));
            }
        }

        /**
         * \brief 获取版本信息数据
         */
        public string GetVersionMessage(Assembly assembly, List<ErrorInfo> errorList)
        {
            String versionMessage = String.Empty;
            GetVersionData(assembly, errorList);
            versionMessage = String.Format("版本号: {0}, 内部版本号：{1}, 版本用途：{2}", Version, InternalVersion, VersionType);
            return versionMessage;
        }
    }
}
